package com.feng.study.demo;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Zeng ShaoYong
 * @Date: 2021-10-08
 * @Description:
 */
public class RedisDemo {

    @Autowired
    private RedisTemplate redisTemplate; //在MyRedisConfig文件中配置了redisTemplate的序列化之后， 客户端也能正确显示键值对了

    public void test() {
        redisTemplate.multi();
        redisTemplate.opsForValue().set("wujinxing", "lige");
        System.out.println(redisTemplate.opsForValue().get("wujinxing"));
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            UserTest user = new UserTest();
            user.setId(i);
            user.setName(String.format("测试%d", i));
            user.setAge(i + 10);
            map.put(String.valueOf(i), user);
        }
        redisTemplate.opsForHash().putAll("测试", map);
        BoundHashOperations hashOps = redisTemplate.boundHashOps("测试");
        Map map1 = hashOps.entries();
        System.out.println(map1);
        int a=1/0;
        redisTemplate.exec();
    }


    @Data
    static class UserTest implements Serializable {
        private int id;
        private String name;
        private long age;
    }
}
